/*
 * Copyright (C) 2021 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.xuexiang.xuidemo.fragment.zhuanquanquan;

import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import androidx.fragment.app.Fragment;

import com.xuexiang.xaop.annotation.SingleClick;
import com.xuexiang.xpage.annotation.Page;
import com.xuexiang.xpage.enums.CoreAnim;
import com.xuexiang.xui.widget.actionbar.TitleBar;
import com.xuexiang.xuidemo.R;
import com.xuexiang.xuidemo.adapter.zhuanquanquan.ZhuanpanAdapter;
import com.xuexiang.xuidemo.base.BaseFragment;
import com.xuexiang.xuidemo.entity.Decision;
import com.xuexiang.xuidemo.utils.ZhuanpanSPUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

@Page(name = "我的决定",anim = CoreAnim.slide)
public class MyDecisionFragment extends BaseFragment {
    List<Decision> decisions;
    ZhuanpanSPUtils zInstance = ZhuanpanSPUtils.getInstance();
    ZhuanpanAdapter adapter;

    @BindView(R.id.list_mydecision)
    ListView listView;

    @Override
    protected int getLayoutId() {
        return R.layout.mydecision_fragment;
    }

    @Override
    protected void initViews() {
        decisions = zInstance.getAllDecision();
        adapter = new ZhuanpanAdapter(getContext(),R.layout.item_decision,decisions,this);
        listView.setAdapter(adapter);
        /*
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                zInstance.setChoice(decisions.get(position).getId());
                popToBack();
            }
        });
         */
    }

    @Override
    protected TitleBar initTitle() {
        TitleBar titleBar = super.initTitle();
        titleBar.addAction(new TitleBar.ImageAction(R.drawable.ic_decision_add) {
            @Override
            @SingleClick
            public void performAction(View view) {
                Fragment addDecision = openNewPage(AddDecisionFragment.class);
                //addDecision.setFragment(MyDecisionFragment.this);
                int a = 1;
            }
        });
        return titleBar;
    }

    @Override
    public void onResume() {
        super.onResume();
        List<Decision> newDecisions;
        newDecisions = zInstance.getAllDecision();
        if(decisions != null)
            decisions.clear();
        else
            decisions = new ArrayList<>();
        for(Decision d:newDecisions) {
            decisions.add(d);
        }
        if(adapter != null)
            adapter.notifyDataSetChanged();
        else
            adapter = new ZhuanpanAdapter(getContext(),R.layout.item_decision,decisions,this);
        adapter.notifyDataSetChanged();
    }
}
